package com.zyx.yygh.cmn.controller;

import com.zyx.yygh.cmn.service.DictService;
import com.zyx.yygh.common.result.Result;
import com.zyx.yygh.model.cmn.Dict;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zyx
 * @version 1.0
 * @date 2022/1/20 020 12:44
 */
@RestController
//@CrossOrigin
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Resource
    private DictService dictService;

    /**
     * 导入数据
     */
    @PostMapping("importData")
    public Result importData(MultipartFile file) {

        dictService.importData(file);
        return Result.ok();
    }

    /**
     * 导出数据
     *
     * @param response
     */
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) {

        dictService.exportDictData(response);
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){

        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {

        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    /**
     * 根据dictcode和value查询
     */
    @GetMapping("getName/{dictcode}/{value}")
    public String getName(@PathVariable String dictcode,
                          @PathVariable String value) {

        String dictName = dictService.getDictName(dictcode, value);
        return dictName;
    }

    /**
     * 根据value查询
     */
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {

        String dictName = dictService.getDictName("", value);
        return dictName;
    }
}
